'use strict';

// constants 与 actions 在一起
import { ajax, nameSpace} from 'utils/index';

let ns = nameSpace('RING_TAGS_STEP1');
export const GET_DATA = ns('GET_DATA');
export const CREATE_TAG_DONE = ns('CREATE_TAG_DONE');
export const LOAD_START = ns('LOAD_START');
export const LOAD_ERR = ns('LOAD_ERR');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const CLOSE_CHOSE_DIALOG = ns('CLOSE_CHOSE_DIALOG');

export function closeDialog() {
  return {type: CLOSE_DIALOG,visible:false};
}
export function closeChoseDialog() {
  return {type: CLOSE_CHOSE_DIALOG};
}
export function editTagTask(params) {
	if(params.unit=="h"){
		params.countdown*=60;
	}
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		//此功能只能在数据库修改
		ajax({
			  api: 'editTagTask',
			  method: 'GET',
			  data:params
			},(json) => {
			  //成功后
			  dispatch({
				  type: CLOSE_DIALOG
			  });
			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}
}
export function createTag(params) {
	if(params.unit=="h"){
		params.countdown*=60;
	}
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		if(window.workbench){
			//千牛环境执行
			  window.QN.top.invoke( {
					cmd : 'tmall.promotag.tag.apply',
					param : {
					  tag_name:params.tag_name,
					  tag_desc:params.tag_desc,
					  start_time:params.start_time,
					  end_time:params.end_time,
					},
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp);
						  dispatch({
							  type: CLOSE_DIALOG
						  });
					},
					error : function(msg, cmd, param) {
						dispatch({
							type: LOAD_ERR,
							message:json.msg+(json.sub_msg?json.sub_msg:""),
							loginFalse:false
						});
					}
				} );
		}else{
			//web环境执行创建折扣
			ajax({
				  api: 'applyTag',
				  method: 'GET',
				  data:{
					  tag_name:params.tag_name,
					  tag_desc:params.tag_desc,
					  start_time:params.start_time,
					  end_time:params.end_time,
				  }
				},(json) => {
				  //成功后
					ajax({
						  api: 'saveTag',
						  method: 'GET',
						  data:{...params,tag_id:json.tag_id,task_type:"qrcode"}
						},(save_json) => {
						  //成功后
							var newDate = new Date();
								newDate.setTime(params.start_time * 1000);
							const tag_start_time = (newDate.getFullYear()+"-"+(newDate.getMonth()+1)+"-"+newDate.getDate()+" "+newDate.getHours()+":"+newDate.getMinutes()+":"+newDate.getSeconds() );
								newDate.setTime(params.end_time * 1000);
							const tag_end_time = (newDate.getFullYear()+"-"+(newDate.getMonth()+1)+"-"+newDate.getDate()+" "+newDate.getHours()+":"+newDate.getMinutes()+":"+newDate.getSeconds() );
			
							dispatch({
								type: CREATE_TAG_DONE,
								newTask:{...params,
										 tag_start_time,
										 tag_end_time,
										 tag_id:json.tag_id,
										 task_id:save_json.task_id}
							});
						},(save_json) => {
						  //失败后
						  //(json.code==53) W2超时
						  const login = (save_json.code==210);
						  dispatch({
							type: LOAD_ERR,
							message:save_json.msg+(save_json.sub_msg?save_json.sub_msg:""),
							loginFalse:login
							});
					});
				},(json) => {
				  //失败后
				  //(json.code==53) W2超时
				  const login = (json.code==210);
				  dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
					});
			});
		}
	}
}


export function getData() {
  return (dispatch) => {
    dispatch({
      type: GET_DATA,
      data: 'home'
    });
  };
}


